﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sfd.Services;

namespace Sfd.WebApp.Projects
{
    public partial class Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindDatas();
        }

        private void BindDatas()
        {
            var user = Membership.GetUser();
            if (user == null)
                Response.Redirect("List.aspx");
            
            // obtention de l'id du projet
            if (!Request.QueryString.AllKeys.Contains("id"))
                Response.Redirect("List.aspx");

            var userId = GetUserId();
            if (userId == null)
                Response.Redirect("List.aspx");

            Guid projectId;
            if (!Guid.TryParse(Request.QueryString["id"], out projectId))
                Response.Redirect("List.aspx");

            var projectService = new ProjectService(true);
            if (!(projectService.IsUserInProject(userId.Value, projectId) || User.IsInRole("admin")))
                Response.Redirect("List.aspx");
           
            var project = projectService.LoadById(projectId);

            txtCle.Text = project.SharedKey;
            txtCompteAzure.Text = project.AccountName;
            txtContainer.Text = project.ContainerName;
            txtNom.Text = project.FriendlyName;
        }

        private Guid? GetUserId()
        {
            var user = Membership.GetUser();
            if (user == null)
                return null;

            var userId = user.ProviderUserKey;

            return userId is Guid ? (Guid?) userId : null;
        }
    }
}